import { playAudio, electronApi } from "src/assets/utils";

export function setData(state, data) {
  for (const key in data) {
    if (Object.hasOwnProperty.call(state, key)) {
      state[key] = data[key];
    }
  }
}

export function setDemands(state, { userid, type, list }) {
  let isFirst = false;
  if (!state.demands[userid]) {
    state.demands[userid] = {};
  }
  if (!state.demands[userid][type]) {
    state.demands[userid][type] = true;
    isFirst = true;
  }
  if (!state.demands.list) {
    state.demands.list = [];
  }
  const newData = list.filter(
    (v) => state.demands.list.indexOf(v.sourceTaskId) === -1
  );
  if (!isFirst && newData.length > 0) {
    newData.forEach((el, i) => {
      playAudio();
      window.electron.notify(
        {
          title: "新需求",
          subtitle: el.taskTitle,
          body: el.taskDesc,
        },
        {
          demandid: el.sourceTaskId,
          userid: userid,
        }
      );
    });
  }

  // try {
  //   list.forEach((el, i) => {
  //     if (i > 0) {
  //       throw new Error();
  //     }
  //     window.electron
  //       .notify(
  //         {
  //           title: "新需求",
  //           subtitle: el.taskTitle,
  //           body: el.taskDesc,
  //         },
  //         {
  //           demandid: el.sourceTaskId,
  //           userid: userid,
  //         }
  //       )
  //       .onClick(() => {
  //         console.log(el);
  //       });
  //   });
  // } catch (error) {}
  state.demands.list.unshift(...newData.map((v) => v.sourceTaskId));
}

export function setCurrentAccount(state, account) {
  state.currentSelectedAccount = account;
}

export function cacheWorkId(state, data) {
  for (const key in data) {
    if (Object.hasOwnProperty.call(data, key)) {
      state.workIds[key] = data[key];
    }
  }
}

export function setServerMessageCount(state, { userid, count }) {
  state.serverMessageCount[userid] = count;
}
